package cn.yyx.aclservice.security.security;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
 * <p>
 * token管理
 * </p>
 * @author qy
 * @since 2019-11-08
 */
@Component
public class TokenManager {
    private long tokenExpiration = 24*60*60*1000;
    private String tokenSignKey = "123456";
//    public String createToken(String username,String mid) {
//        String token = Jwts.builder().setSubject(username)
//                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
//                .signWith(SignatureAlgorithm.HS512, tokenSignKey).compressWith(CompressionCodecs.GZIP).compact();
//        return token;
//    }
    //生成token字符串的方法
    public   String createToken(String username,String mid){
        String JwtToken = Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .setSubject(username)
                .setIssuedAt(new Date())
                .claim("username", username)  //设置token主体部分 ，存储用户信息
                .signWith(SignatureAlgorithm.HS256, tokenSignKey)
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
                .compact();
        return JwtToken;
    }
    public String getUserFrsomToken(String token) {
        String user = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token).getBody().getSubject();
        return user;
    }
    public void removeToken(String token) {
        //jwttoken无需删除，客户端扔掉即可。
    }
}
